{extend name="light-year/layout/master/detail" /}

{block name="title"}角色管理{/block}
{block name="page-title"}角色{/block}

{block name="content"}
    <input name="role_id" class="form-control" type="hidden" value="{if $detail}{$detail->role_id}{/if}" />
    <div class="form-group">
        <label><span class="text-danger">*</span> 角色名称：</label>
        <input type="text" class="form-control" name="role_name" value="{if $detail}{$detail->role_name}{/if}" placeholder="输入角色名称" required="" />
        <small class="help-block">
            <i class="mdi mdi-information"></i>
            唯一标识
        </small>
    </div>
    <div class="form-group">
        <label>
            是否启用：
        </label>
        <div class="clearfix">
            <label class="lyear-radio radio-inline radio-primary">
                <input type="radio" name="is_check" value="1" {if (empty($detail) || $detail->is_check === 1) }checked{/if}><span>启用</span>
            </label>
            <label class="lyear-radio radio-inline radio-primary">
                <input type="radio" name="is_check" value="0" {if (empty($detail) || $detail->is_check === 0) }checked{/if}><span>禁用</span>
            </label>
        </div>
    </div>
    <div class="form-group">
        <label> 备注：</label>
        <textarea class="form-control" rows="5" name="role_remarks" placeholder="请输入备注" >{if $detail}{$detail->role_remarks}{/if}</textarea>
    </div>
    <div class="form-group">
        <label>
            设置权限：
        </label>
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                <tr>
                    <th>
                        <label class="lyear-checkbox checkbox-primary">
                            <input name="checkbox" type="checkbox" id="check-all">
                            <span> 全选</span>
                        </label>
                    </th>
                </tr>
                </thead>
                <tbody>
                    {if !empty($menus)}
                        <?php
                            $menu_ids = array_column($detail->menus->toArray(), 'menu_id', 'menu_id');
                        ?>
                        {foreach $menus as $menu}
                            <tr>
                                <td>
                                    <label class="lyear-checkbox checkbox-primary">
                                        <input name="menu_ids[]" type="checkbox" class="checkbox-parent"
                                               dataid="id-{$menu['menu_id']}" value="{$menu['menu_id']}"
                                               {if isset($menu_ids[$menu['menu_id']])}checked{/if}
                                            />
                                        <span> {$menu['menu_name']} </span>
                                    </label>
                                </td>
                            </tr>
                            {if !empty($menu['_child'])}
                                {foreach $menu['_child'] as $child}
                                    <tr>
                                        <td class="p-l-40">
                                            <label class="lyear-checkbox checkbox-primary">
                                                <input name="menu_ids[]" type="checkbox" class="checkbox-parent checkbox-child"
                                                    dataid="id-{$menu['menu_id']}-{$child['menu_id']}"
                                                    value="{$child['menu_id']}"
                                                    {if isset($menu_ids[$child['menu_id']])}checked{/if}
                                                    />
                                                <span> {$child['menu_name']} </span>
                                            </label>
                                        </td>
                                    </tr>
                                    {if !empty($child['_child'])}
                                        <tr>
                                            <td class="p-l-80">
                                                {foreach $child['_child'] as $third}
                                                    <label class="lyear-checkbox checkbox-primary checkbox-inline checkbox-inline checkbox-inline">
                                                        <input name="menu_ids[]" type="checkbox" class="checkbox-child"
                                                               dataid="id-{$menu['menu_id']}-{$child['menu_id']}-{$third['menu_id']}" value="{$third['menu_id']}"
                                                               {if isset($menu_ids[$third['menu_id']])}checked{/if}
                                                            />
                                                        <span> {$third['menu_name']} </span>
                                                    </label>
                                                {/foreach}
                                            </td>
                                        </tr>
                                    {/if}
                                {/foreach}
                            {/if}
                        {/foreach}
                    {/if}
                </tbody>
            </table>
        </div>
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary ajax-post" target-form="add-form">确 定</button>
        <button type="button" class="btn btn-default" onclick="javascript:history.back(-1);return false;">返 回</button>
    </div>
{/block}

{block name="script-src"}
{/block}

{block name="script"}
<script type="text/javascript">
    window.onload = function () {
        closeLayerLoading(closeAllLayer);

        //动态选择框，上下级选中状态变化
        $('input.checkbox-parent').on('change', function () {
            var dataid = $(this).attr("dataid");
            $('input[dataid^=' + dataid + '-]').prop('checked', $(this).is(':checked'));
        });
        $('input.checkbox-child').on('change', function () {
            var dataid = $(this).attr("dataid");
            dataid = dataid.substring(0, dataid.lastIndexOf("-"));
            var parent = $('input[dataid=' + dataid + ']');
            if ($(this).is(':checked')) {
                parent.prop('checked', true);
                //循环到顶级
                while (dataid.lastIndexOf("-") != 2) {
                    dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                    parent = $('input[dataid=' + dataid + ']');
                    parent.prop('checked', true);
                }
            } else {
                //父级
                if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                    parent.prop('checked', false);
                    //循环到顶级
                    while (dataid.lastIndexOf("-") != 2) {
                        dataid = dataid.substring(0, dataid.lastIndexOf("-"));
                        parent = $('input[dataid=' + dataid + ']');
                        if ($('input[dataid^=' + dataid + '-]:checked').length == 0) {
                            parent.prop('checked', false);
                        }
                    }
                }
            }
        });
    }
</script>
{/block}